<?php

namespace xing\verify\drive;

use xing\helper\resource\HttpHelper;
use xing\verify\BaseVerify;
use xing\verify\interfaces\VerifyMobileInterface;

class BackgroundCheckAliAnNa extends BaseVerify implements VerifyMobileInterface
{
    private $appKey;
    private $appSecret;
    private $appCode;

    public function init($config)
    {
        $this->config = $config;
        $this->appKey = $config['appKey'] ?? null;
        $this->appSecret = $config['appSecret'] ?? null;
        $this->appCode = $config['appCode'];
        return $this;
    }

    public function query($mobile, $name, $cardNumber)
    {
        if (empty($cardNumber)) throw new \Exception('身份证号为空');
        if (empty($name)) throw new \Exception('姓名为空');
        if (empty($mobile)) throw new \Exception('手机号为空');

        $appcode = $this->appCode;
        $headers = ["Authorization:APPCODE " . $appcode];
        $post = [
            'idcard_number' => $cardNumber,
            'mobile_number' => $mobile,
            'name' => $name
        ];
        $url = "https://jumjokk.market.alicloudapi.com/personal/disenforcement";
        $result = $this->result = json_decode(HttpHelper::post($url, $post, $headers), true);
        if (empty($result)) {
            throw new \Exception('查询无结果'); // 可能是没钱了
        }
        if ($result['code'] != 200) {
            throw new \Exception($result['msg'], $result['code']);
        }
        $list = isset($result['data']['caseList']) ? $result['data']['caseList'] : [];
        // 格式化返回数据
        $return = [];
        foreach ($list as $k => $v) {
            $return[$k] = [
                'age' => $v['age'],
                'provinceName' => $v['areaname'],
                'caseNo' => $v['casecode'],
                'courtName' => $v['courtname'],
                'name' => $v['iname'],
                'date' => $v['publishdate'],
                'registerDate' => $v['regdate'],
                'sex' => $v['sexname'] == '女性' ? 0 : 1
            ];
        }

        return $return;
    }
}
